
package com.loong.loong.hik.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.loong.common.core.util.R;
import com.loong.loong.hik.entity.DoorPoint;
import com.loong.loong.hik.mapper.DoorPointMapper;
import com.loong.loong.hik.property.request.door.DoorControlRequest;
import com.loong.loong.hik.property.result.door.DoorControlResult;
import com.loong.loong.hik.property.urls.face.HikFaceUrl;
import com.loong.loong.hik.service.DoorPointService;
import com.loong.loong.hik.service.HikOneCardVService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 门禁点
 *
 * @author jinyiang
 * @date 2023-03-29 17:23:58
 */
@Service
public class DoorPointServiceImpl extends ServiceImpl<DoorPointMapper, DoorPoint> implements DoorPointService {

	@Resource
	private HikOneCardVService hikOneCardVService;
	/**
	 * 控制门禁
	 * @param doorCode
	 * @param num
	 * @return
	 */
	@Override
	public R controller(String doorCode, Integer num) {
//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			DoorControlRequest doorControlRequest = new DoorControlRequest();
			List<String> list = new ArrayList<>();
			list.add(doorCode);
			doorControlRequest.setDoorIndexCodes(list);
			doorControlRequest.setControlType(num);
			try {
				DoorControlResult doorControlResult = hikOneCardVService.doorControl(doorControlRequest);

				if (doorControlResult!=null&&"0".equals(doorControlResult.getCode())){
					return R.ok();
				}else{
					return R.failed("接口调用失败"+(StringUtils.isNotEmpty(doorControlResult.getMsg())?doorControlResult.getMsg():""));
				}

			} catch (Exception e) {
				e.printStackTrace();
				return R.failed("接口调用异常"+e.getMessage());
			}

		}
		DoorPoint doorPoint = new DoorPoint();
		doorPoint.setDoorCode(doorCode);
		DoorPoint one = getOne(Wrappers.query(doorPoint));
		if (one!=null){
			one.setDoorStatus(num.toString());
		}
		return R.ok(updateById(one));
	}
}
